<template>
  <!-- 发票管理 -->
  <div class="page-order-invoice">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="orderNumber"
            :label="$t('order.number') + ':'"
          >
            <el-input
              v-model="searchForm.orderNumber"
              type="text"
              clearable
              :placeholder="$t('order.number')"
            />
          </el-form-item>
          <el-form-item
            prop="invoiceState"
            :label="$t('order.invoiceStatus') + ':'"
          >
            <el-select
              v-model="searchForm.invoiceState"
              clearable
              :placeholder="$t('order.invoiceStatus')"
            >
              <el-option
                v-for="item in invoiceStates"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch()"
            >
              {{ $t('shop.resetMap') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 搜索栏end -->

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con invoice-table">
        <el-table
          ref="InvoiceTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          style="width: 100%"
        >
          <!-- 订单id -->
          <el-table-column
            :label="$t('order.number')"
            prop="orderNumber"
            width="180"
          >
            <template #default="{ row }">
              <span>{{ row.orderNumber || '-' }}</span>
            </template>
          </el-table-column>
          <!-- 抬头类型 1.单位 2.个人 -->
          <el-table-column
            :label="$t('order.headerType')"
            prop="headerType"
          >
            <template #default="{ row }">
              <span>{{ ['', $t('order.unit'), $t('order.personal')][row.headerType] || '-' }}</span>
            </template>
          </el-table-column>
          <!-- 发票状态 1.申请中 2.已开票 -->
          <el-table-column
            :label="$t('order.invoiceStatus')"
            prop="invoiceState"
          >
            <template #default="{ row }">
              <span>{{
                ['', $t('order.applicationInProgress'), $t('order.invoiceIssued')][row.invoiceState]
              }}</span>
            </template>
          </el-table-column>
          <!-- 订单状态 -->
          <el-table-column
            :label="$t('order.orderStatus')"
            prop="orderStatus"
          >
            <template #default="scope">
              <span v-if="scope.row.orderStatus === 1">{{ $t('order.pendingPayment') }}</span>
              <span v-if="scope.row.orderStatus === 2">{{ $t('order.toBeShipped') }}</span>
              <span v-if="scope.row.orderStatus === 3">{{ $t('order.pendingReceipt') }}</span>
              <span v-if="scope.row.orderStatus === 4">{{ $t('order.toBeEvaluated') }}</span>
              <span v-if="scope.row.orderStatus === 5">{{ $t('order.success') }}</span>
              <span v-if="scope.row.orderStatus === 6">{{ $t('order.fail') }}</span>
              <span v-if="scope.row.orderStatus === 7">{{ $t('group.waitGroup') }}</span>
            </template>
          </el-table-column>
          <!-- 申请时间 -->
          <el-table-column
            :label="$t('order.applicationTime')"
            prop="applicationTime"
            width="150"
          >
            <template #default="{ row }">
              <span>{{ row.applicationTime || '-' }}</span>
            </template>
          </el-table-column>
          <!-- 上传时间 -->
          <el-table-column
            width="150"
            :label="$t('order.uploadTime')"
            prop="uploadTime"
          >
            <template #default="{ row }">
              <span>{{ row.uploadTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('crud.menu')"
            fixed="right"
            align="center"
            width="200"
          >
            <template #default="{ row }">
              <div
                v-if="isAuth('order:order:info')"
                class="default-btn text-btn"
                @click="onViewOrderHandle(row.orderNumber)"
              >
                {{ $t('order.viewOrder') }}
              </div>
              <div
                v-if="isAuth('order:orderInvoice:update')"
                class="default-btn text-btn"
                @click="onAddOrUpdate(row.orderInvoiceId)"
              >
                {{ $t('coupon.edit') }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />

    <!-- 订单详情 -->
    <order-info
      v-if="orderInfoVisible"
      ref="OrderInfoRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'
import OrderInfo from '../order/add-or-update.vue'
import { isAuth } from '@/utils'

const invoiceStates = [
  {
    label: $t('order.applicationInProgress'),
    value: 1
  },
  {
    label: $t('order.invoiceIssued'),
    value: 2
  }
]

onMounted(() => {
  onGetDataList()
})

// 获取数据列表
let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = ref({})
const onGetDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/m/orderInvoice/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam ? pageParam.currentPage : page.value.currentPage,
          size: pageParam ? pageParam.pageSize : page.value.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.value.total = data.total
  })
}

// 条件查询
const onSearch = (newData = false) => {
  page.value.currentPage = 1
  onGetDataList(page.value, newData)
}

/**
 * 查看订单
 */
const orderInfoVisible = ref(false) // 订单详情弹窗显隐

const OrderInfoRef = ref(null) // 拿子组件方法
const onViewOrderHandle = id => {
  orderInfoVisible.value = true
  nextTick(() => {
    OrderInfoRef.value?.init(id)
  })
}
// 新增 / 修改
const addOrUpdateVisible = ref(false) // 编辑弹窗显隐
const addOrUpdateRef = ref(null) // 拿子组件方法
const onAddOrUpdate = id => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}

const onPageSizeChange = val => {
  page.value.pageSize = val
  onGetDataList(page.value)
}
const onPageChange = val => {
  page.value.currentPage = val
  onGetDataList(page.value)
}

// 重置
const onResetSearch = () => {
  searchForm.value = {}
}
// 刷新回调用
const refreshChange = () => {
  page.value.current = 1
  onGetDataList(page.value)
}
</script>

<style lang="scss" scoped>
.page-order-invoice {
  :deep(.el-input__inner) {
    border: 0;
  }
}
</style>
